﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%--<!--ztree-->
<link rel="stylesheet" href="../../Scripts/zTree/css/demo.css" type="text/css" />
<link rel="stylesheet" href="../../Scripts/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css" />

<script type="text/javascript" src="../../Scripts/zTree/js/jquery.ztree.core-3.0.js"></script>

<script type="text/javascript" src="../../Scripts/zTree/js/jquery.ztree.excheck-3.0.js"></script>

<script type="text/javascript" src="../../Scripts/zTree/js/jquery.ztree.exedit-3.0.js"></script>--%>

<script type="text/javascript">
    var appendixsetting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };


    var treeObj;
    var usertreeObj;
    var tabs = null;
    var editor = null;
    $(function() {
        intityUserTree_MsgSend();
        intityAppendixTree_MsgSend();
    });

    function intityAppendixTree_MsgSend() {
        var appendixNode = $("#appendixNodemsg").val();
        $.fn.zTree.init($("#appendixZtreemsg"), appendixsetting, eval(appendixNode));
        treeObj = $.fn.zTree.getZTreeObj("appendixZtreemsg");
    }

    function intityUserTree_MsgSend() {
        //部门
        $.ajax({
            url: "/Home/getDeptUserCheckTree",
            type: "POST",
            success: function(userNodes) {
                setting.callback.onCheck = onUsertreeCheck_MsgSend;
                $("#div_saved_tree").hide();
                $("#div_lxr_tree").show();
                $.fn.zTree.init($("#userdept_msg_tree"), setting, eval(userNodes));
            }
        });

        //职位
        $.ajax({
            url: "/Home/getDutyUserCheckTree",
            type: "POST",
            success: function(userNodes) {
                setting.callback.onCheck = onUsertreeCheck_MsgSend;
                $.fn.zTree.init($("#userduty_msg_tree"), setting, eval(userNodes));
            }
        });
        tabs = $("#tabs_msgsend").tabs();
    }

    function submit() {
        var title = $("#title").val();
        var content = CKEDITOR.tools.htmlEncode(editor.getData());
        var content2 = editor.document.getBody().getText(); 
        var receiver_ids = $("#receiver_ids").val();
        var creater_id = $("#creater_id").val();
        var appdixname = $("#appendixNamemsg").html();
        if (receiver_ids.length == 0) {
            alert("请选择收件人");
            return false;
        }
        if (title == "") {
            alert("请输入主题");
            return false;
        }
        var msgId = $("#msg_id").val();
        var appendix = $("#appendixHimsg").val();

        $.ajax({
            url: "/Message/DoSendMsg",
            type: "POST",
            data: { "title": title, "content": content, "content2": content2, "receiver_ids": receiver_ids, "creater_id": creater_id, "appendix": appendix, "msgId": msgId, "appendixNamemsg": appdixname },
            success: function(restag) {
                if (restag.res) {
                    $("#msg_id").val("");
                    alert("发送成功！");
                    $.fn.deltab('msg-1'); //关闭当前tab
                }
                else
                    alert(restag.msg);
            }
        });

    }

    var savedsetting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: getSavedMsg
        }
    };

    function getsavedmsgTree() {
        $.ajax({
            type: "POST",
            url: "/Message/GetSavedMsgs",
            success: function(msgTree) {
                if (msgTree != "") {
                    $("#div_saved_tree").show();
                    $("#div_lxr_tree").hide();

                    $("#div_tree").parent().children("div:eq(0)").html("选择信件");
                    $.fn.zTree.init($("#saved_msg_tree"), savedsetting, eval(msgTree));
                } else {
                    alert("您未保存信件");
                }
            }
        })
    }

    function getSavedMsg(event, treeId, treeNode) {
        var msgId = treeNode.id;
        if (msgId != "List") {
            setSavedMsg(msgId);

            $("#msg_id").val(msgId);
            $("#div_tree").parent().children("div:eq(0)").html("选择联系人");
            intityUserTree_MsgSend();
        }
    }

    $(function() {
        var msgId = $("#msg_id").val();
        if (msgId != "null" && msgId != "") {
            setSavedMsg(msgId);
        }
    })

    function setSavedMsg(msgId) {
        $.getJSON(
            "/Message/GetSavedMsg",
            { "msgId": msgId },
            function(json) {
                var operType = $("#operType").val();
                if (operType != 'repeat') {
                    $("#receiver_ids").val(json.receiver_ids);
                    $("#receiver_names").val(json.receiver_names);
                }
                $("#title").val(json.title);
                editor.setData(json.content);
                $("#appendixHimsg").val(json.appendix);
                $("#appendixNamemsg").html(json.appendix_name);
            }
        )
    }

    function savemsg() {
        var title = $("#title").val();
        var content = CKEDITOR.tools.htmlEncode(editor.getData());
        var content2 = editor.document.getBody().getText();
        var receiver_ids = $("#receiver_ids").val();
        var creater_id = $("#creater_id").val();
        var appendix = $("#appendixHimsg").val();
        var msgId = $("#msg_id").val();
        var appendix_name = $("#appendixNamemsg").html();
        
        $.ajax({
            url: "/Message/SaveMsg",
            type: "POST",
            data: { "title": title, "content": content, "content2": content2, "receiver_ids": receiver_ids, "creater_id": creater_id, "appendix": appendix, "msgId": msgId, "appName": appendix_name },
            success: function(restag) {
                $("#msg_id").val("");
                if (restag.res) {
                    alert("保存成功！");
                }
                else
                    alert(restag.msg);
            }
        });
    }

    function onUsertreeCheck_MsgSend(event, treeId, treeNode) {
        //alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
        //存储结果变量
        var temp_name = new Array();
        var temp_id = new Array();

        var treeObj = $.fn.zTree.getZTreeObj("userdept_msg_tree");
        var nodes = treeObj.getCheckedNodes(true);

        for (i = 0; i < nodes.length; i++) {
            if (nodes[i].level == 2) {
                temp_id.push(nodes[i].id);
                temp_name.push(nodes[i].name);
                //temp_id = temp_id + nodes[i].id + ",";
                //temp_name = temp_name + nodes[i].name + ",";
            }
        }
        treeObj = $.fn.zTree.getZTreeObj("userduty_msg_tree");
        nodes = treeObj.getCheckedNodes(true);
        for (i = 0; i < nodes.length; i++) {
            if (nodes[i].level == 2) {
                temp_id.push(nodes[i].id);
                temp_name.push(nodes[i].name);
                //temp_id = temp_id + nodes[i].id + ",";
                //temp_name = temp_name + nodes[i].name + ",";
            }
        }
        temp_id = temp_id.unique();
        temp_name = temp_name.unique();

        $("#receiver_ids").attr("value", temp_id.join(","));
        $("#receiver_names").attr("value", temp_name.join(","));
    };




    // 添加附件
    function addAppendixmsg() {
        var nodes = treeObj.getCheckedNodes(true);
        var appendixName = "";
        var appendix = "";

        for (var index = 0; index < nodes.length; index++) {
            if (nodes[index].id == "appendix")
                continue;
            appendixName += nodes[index].name + " ";
            appendix += nodes[index].id + ";";
        }
        $("#appendixNamemsg").html(appendixName);
        $("#appendixHimsg").val(appendix);
        $("#appendixmsg").fadeOut("slow");
    }

    // 显示附件窗口
    function showAppendixmsg() {
        $("#appendixmsg").fadeIn("slow");
    }

    // 隐藏附件窗口
    function cancelmsg() {
        $("#appendixmsg").fadeOut("slow");
    }
</script>

<div id="msgsend" style="width: 1100px;">
    <!--增加上外边界-->
    <div id="hidearea" style="text-align: right;">
        <%=Html.Hidden("creater_id", ViewData["creater_id"]) %>
        <%=Html.Hidden("receiver_ids", ViewData["receiver_ids"]) %>
        <%=Html.Hidden("msg_id", ViewData["msg_id"])%>
        <%=Html.Hidden("operType",ViewData["operType"]) %>
    </div>
    <div>
        <input class="btn_style" type="button" onclick="getsavedmsgTree();" value="打开保存信件" />
    </div>
    <div style="float: left; margin-top: 14px; clear: left; line-height: 25px; margin-top: 10px">
        <div class="message_body" style="width: 775px; padding-left: 12px;">
            收件人:<%=Html.TextBox("receiver_names", ViewData["receiver_names"], new { style = "width:700px; margin-left:6px; border:solid 1px #6C92AD;",@readonly="readonly" })%><br />
            主&nbsp;&nbsp;&nbsp;题:<input type="text" id="title" style="width: 700px; margin-left: 4px;
                margin-top: 5px; margin-bottom: 5px; border: solid 1px #6C92AD;" /><br />
            内&nbsp;&nbsp;&nbsp;容:<br />
            <%=Html.Partial("Fckeditor") %>
            <div>
                <div style="width: 52px; margin-top: 2px; float: left; display: block;">
                    附&nbsp;&nbsp;&nbsp;件:</div>
                <div id="appendixNamemsg" style="margin-top: 3px; width: 700px; float: left; height: 20px;
                    border: solid 1px #6C92AD;">
                </div>
                <br />
                <input class="btn_style" type="button" onclick="showAppendixmsg();" style="margin-left: -55px;
                    margin-top: 5px;" value="附件" />
                <input class="btn_style" type="button" value="发送" onclick="return submit();" style="margin-top: 5px;
                    margin-left: 10px;" />
                <input class="btn_style" type="button" value="保存" onclick="return savemsg();" style="margin-top: 5px;
                    margin-left: 10px;" />
                <div class="divBox" id="appendixmsg" style="display: none;">
                    <div id="appendixZtreemsg" class="ztree">
                    </div>
                    <input type="hidden" id="appendixNodemsg" value="<%=ViewData["appendix"] %>" />
                    <input id="appendixHimsg" type="hidden" /><br />
                    <input class="btn_style" type="button" onclick="addAppendixmsg();" value="确定" />
                    <input class="btn_style" type="button" onclick="cancelmsg()" value="取消" />
                </div>
            </div>
        </div>
    </div>
    <!--选择树-->
    <div id="div_saved_tree" class="ztree_body" style="float: left; display: none;">
        <div style="width: 230px;">
            选择信件</div>
        <div id="div_tree" class="zTreeDemoBackground left" style="float: left; width: 230px;">
            <ul id="saved_msg_tree" class="ztree">
            </ul>
        </div>
    </div>
    <div id="div_lxr_tree" class="ztree_body" style="float: left">
        <div style="width: 230px;">
            选择联系人</div>
        <div id="tabs_msgsend">
            <ul>
                <li><a href="#tabs-1" style="font-size: 11px">员工(部门)</a></li>
                <li><a href="#tabs-2" style="font-size: 11px">员工(职位)</a></li>
            </ul>
            <div id="tabs-1">
                <div id="div_tree1" class="zTreeDemoBackground left" style="width: 230px; margin-left: -10px">
                    <ul id="userdept_msg_tree" class="ztree" style="height: 355px;">
                    </ul>
                </div>
            </div>
            <div id="tabs-2">
                <div id="div_tree2" class="zTreeDemoBackground left" style="width: 230px; margin-left: -10px">
                    <ul id="userduty_msg_tree" class="ztree" style="height: 355px;">
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
